package com.space.service.impl;

import com.space.dao.ExpenseDao;
import com.space.entity.Expense;
import com.space.service.ExpenseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : 港某
 * @date : 2020/2/16 12:28
 */
@Service
public class ExpenseServiceImpl implements ExpenseService {

    @Autowired
    ExpenseDao expenseDao;

    @Override
    public List<Expense> selectAllExpense() {
        return expenseDao.selectAllExpense();
    }

    @Override
    public List<Expense> selectAllExpenseByUserId(int userId) {
        return expenseDao.selectAllExpenseByUserId(userId);
    }

    @Override
    public Expense selectExpenseById(int expenseId, int userId) {
        Map<String, Integer> tmp = new HashMap<>(3);
        tmp.put("userId", userId);
        tmp.put("expenseId", expenseId);
        return expenseDao.selectExpenseById(tmp);
    }

    @Override
    public void deleteExpense(int expenseId, int userId) {
        Map<String, Integer> map = new HashMap<>(3);
        map.put("userId", userId);
        map.put("expenseId", expenseId);
        Expense expense = expenseDao.selectExpenseById(map);
        if (expense.getExpenseFlight().getFlightStatus().getStatusTypeId() == 1) {
            expenseDao.deleteExpense(expenseId);
        }
    }

    @Override
    public void updateExpenseFlightById(int expenseId, int flightId) {
        Map<String, Integer> map = new HashMap<>(3);
        map.put("flightId", flightId);
        map.put("expenseId", expenseId);
        expenseDao.updateExpenseFlightById(map);
    }
}
